﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.ServiceModel;

/// <summary>
/// Summary description for CategoryEntity
/// </summary>
public class CategoryEntity
{
	public CategoryEntity()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    [DataMember]
    public long ID { set; get; }

    [DataMember]
    public string Name { set; get; }

    [DataMember]
    public string Description { set; get; }

    [DataMember] 
    public List<CategoryDetailEntity> categoryDetails { set; get;}

    [DataMember]
    public long Items { set; get; }

    public CategoryEntity(Category item,long platformID)
    {
        this.ID = item.ID;
        this.Name = item.Name;
        this.Description = item.Description;

        loadCategoryDetail();
        Items = loadItemBooks(ID, platformID);
    }

    public CategoryEntity(long ID, string name)
    {
        this.ID = ID;
        this.Name = Name;
    }

    public CategoryEntity(Category item)
    {
        this.ID = item.ID;
        this.Name = item.Name;
        this.Description = item.Description;

        loadCategoryDetail();
        Items = loadItemBooks();
    }
    public void loadCategoryDetail()
    {
         try
        {
            BookContextDataContext bookContext = new BookContextDataContext();
            List<CategoryDetail> list = bookContext.CategoryDetails.Where(c => c.CategoryID == ID).ToList();
            categoryDetails = new List<CategoryDetailEntity>();
            foreach (CategoryDetail item in list)
            {
                categoryDetails.Add(new CategoryDetailEntity(item));
            }
        }
        catch
        {}
    }

    public long loadItemBooks()
    {
        try
        {
            BookContextDataContext bookContext = new BookContextDataContext();
            long num = bookContext.Books.Where(c => c.CategoryID == ID).ToList().Count;
            return num;
        }
        catch
        {
            return 0;
        }
    }
    public long loadItemBooks(long categoryID, long platformID)
    {
        try
        {
            BookContextDataContext bookContext = new BookContextDataContext();
            var list = from b in bookContext.Books
                       join c in bookContext.Categories on b.CategoryID equals c.ID
                       join cd in bookContext.CategoryDetails on c.ID equals cd.CategoryID
                       where cd.PlatformID == platformID && cd.CategoryID == categoryID
                       select b;
            long num = list.Count();
            return num;
        }
        catch
        {
            return 0;
        }
    }
}